<?php
namespace library\tencent\wechat\admin_wechat;

use library\tencent\wechat\common\WechatMP;
use think\Db;
class SendTemplateMessage
{
    //买家通知--订单通知--订单成功提交
    public static function send_message($tem_id_short,array $info,$url=null){
        $item=Db::name('wechat_template_message')->where(['template_short_id'=>$tem_id_short])->find();
        if($item['status']==0){
            return false;
        }
        switch ($tem_id_short){
            case "OPENTM200746866"://订单提交成功通知
               return self::order_success_template($item,$info,$url);
                break;
            case "OPENTM406590003"://订单开始配送通知
                return self::delivery_template($item,$info,$url);
                break;
            case "OPENTM201594720"://自提订单提交成功通知
                return  self::order_by_oneself_success($item,$info,$url);
                break;
            case "TM00850"://订单取消通知
                return self::cancel_order($item,$info,$url);
                break;
            case "OPENTM204987032"://订单支付成功通知
                return self::pay_success_order($item,$info,$url);
                break;
            case "OPENTM202243318"://订单发货通知
                return self::deliver_goods($item,$info,$url);
                break;
            case "OPENTM202314085"://订单确认收货通知
                return self::take_over_confirm($item,$info,$url);
                break;
//            case "OPENTM207793687"://自动发货通知
//                return self::cancel_order($tem_id_short,$info,$url);
//                break;
//            case "TM00017"://订单状态更新(修改收货地址)(修改价格)
//                return self::cancel_order($tem_id_short,$info,$url);
//                break;
            case "TM00431"://退款申请通知
                return self::refund_apply($item,$info,$url);
                break;
            case "OPENTM401479948"://退款进度通知
                return self::rate_of_refund($item,$info,$url);
                break;
            case "TM00430"://退款成功通知
                return self::refund_success($item,$info,$url);
                break;
            case "TM00432"://退款申请驳回通知
                return self::refund_reject($item,$info,$url);
                break;
            case "TM00977"://充值成功通知
                return self::cancel_order($item,$info,$url);
                break;
//            case "TM00004"://充值退款通知
//                return self::cancel_order($item,$info,$url);
//                break;
//            case "TM00979"://提现提交通知
//                return self::cancel_order($item,$info,$url);
//                break;
//            case "TM00980"://提现成功通知
//                return self::cancel_order($item,$info,$url);
//                break;
//            case "TM00981"://提现失败通知
//                return self::cancel_order($item,$info,$url);
//                break;
            case "OPENTM200605630"://会员升级通知(任务处理通知)
                return self::level_up($item,$info,$url);
                break;
            case "TM00211"://返现成功通知
                return self::cash_back($item,$info,$url);
                break;
            case "OPENTM208036452"://配送员待处理订单通知
                return self::delivery_pending($item,$info,$url);
                break;
            case "OPENTM207498902"://订单付款通知（卖家通知）
                return self::msg_to_seller($item,$info,$url);
                break;
        }
    }

    protected static function msg_to_seller($item,array $info,$url=null){
        if (empty($item)){
            return false;
        }
        if (empty($url)){
            $url = $item['url'];
        }
        $data=json_decode(str_replace(array('$keyword1','$keyword2','$keyword3','$keyword4'),array($info['keyword1'],$info['keyword2'],$info['keyword3'],$info['keyword4']),$item['content']),true);
        $access_token=WechatMpService::getAccessToken();
        return WechatMP::sendTemplateMessage($access_token,$info['touser'],$item['template_id'],$url,$data);
    }

    protected static function delivery_pending($item,array $info,$url=null){
        if (empty($item)){
            return false;
        }
        if (empty($url)){
            $url = $item['url'];
        }
        $data=json_decode(str_replace(array('$keyword1','$keyword2','$keyword3','$keyword4','$keyword5'),array($info['keyword1'],$info['keyword2'],$info['keyword3'],$info['keyword4'],$info['keyword5']),$item['content']),true);
        $access_token=WechatMpService::getAccessToken();
        return WechatMP::sendTemplateMessage($access_token,$info['touser'],$item['template_id'],$url,$data);
    }


    protected static function cash_back($item,array $info,$url=null){
        if (empty($item)){
            return false;
        }
        if (empty($url)){
            $url = $item['url'];
        }
        $data=json_decode(str_replace(array('$order','$money'),array($info['order'],$info['money']),$item['content']),true);
        $access_token=WechatMpService::getAccessToken();
        return WechatMP::sendTemplateMessage($access_token,$info['touser'],$item['template_id'],$url,$data);
    }


    protected static function order_success_template($item,array $info,$url=null){
        if (empty($item)){
            return false;
        }
        if (empty($url)){
            $url = $item['url'];
        }
        $data=json_decode(str_replace(array('$keyword1','$keyword2','$keyword3','$keyword4'),array($info['keyword1'],$info['keyword2'],$info['keyword3'],$info['keyword4']),$item['content']),true);
        $access_token=WechatMpService::getAccessToken();
        return WechatMP::sendTemplateMessage($access_token,$info['touser'],$item['template_id'],$url,$data);
    }
    protected static function delivery_template($item,array $info,$url=null){
        if (empty($item)){
            return false;
        }
        if (empty($url)){
            $url = $item['url'];
        }
        $data=json_decode(str_replace(array('$keyword1','$keyword2','$keyword3','$keyword4'),array($info['keyword1'],$info['keyword2'],$info['keyword3'],$info['keyword4']),$item['content']),true);
        $access_token=WechatMpService::getAccessToken();
        return WechatMP::sendTemplateMessage($access_token,$info['touser'],$item['template_id'],$url,$data);
    }
    protected static function order_by_oneself_success($item,$info,$url=null){
        if (empty($item)){
            return false;
        }
        if (empty($url)){
            $url = $item['url'];
        }
        $data=json_decode(str_replace(array('$keyword1','$keyword2','$keyword3','$keyword4'),array($info['keyword1'],$info['keyword2'],$info['keyword3'],$info['keyword4']),$item['content']),true);
        $access_token=WechatMpService::getAccessToken();
        return WechatMP::sendTemplateMessage($access_token,$info['touser'],$item['template_id'],$url,$data);
    }

    protected static function cancel_order($item,$info,$url=null){
        if (empty($item)){
            return false;
        }
        if (empty($url)){
            $url = $item['url'];
        }
        $data=json_decode(str_replace(array('$orderProductPrice','$orderProductNam','$orderAddress','$orderName'),array($info['orderProductPrice'],$info['orderProductNam'],$info['orderAddress'],$info['keyword4']),$item['orderName']),true);
        $access_token=WechatMpService::getAccessToken();
        return WechatMP::sendTemplateMessage($access_token,$info['touser'],$item['template_id'],$url,$data);
    }

    protected static function pay_success_order($item,$info,$url=null){
        if (empty($item)){
            return false;
        }
        if (empty($url)){
            $url = $item['url'];
        }
        $data=json_decode(str_replace(array('$keyword1','$keyword2','$keyword3','$keyword4','$keyword5'),array($info['keyword1'],$info['keyword2'],$info['keyword3'],$info['keyword4'],$info['keyword5']),$item['content']),true);
        $access_token=WechatMpService::getAccessToken();
        return WechatMP::sendTemplateMessage($access_token,$info['touser'],$item['template_id'],$url,$data);
    }

    protected static function deliver_goods($item,$info,$url=null){
        if (empty($item)){
            return false;
        }
        if (empty($url)){
            $url = $item['url'];
        }
        $data=json_decode(str_replace(array('$keyword1','$keyword2','$keyword3','$keyword4'),array($info['keyword1'],$info['keyword2'],$info['keyword3'],$info['keyword4']),$item['content']),true);
        $access_token=WechatMpService::getAccessToken();
        return WechatMP::sendTemplateMessage($access_token,$info['touser'],$item['template_id'],$url,$data);
    }

    protected static function take_over_confirm($item,$info,$url=null){
        if (empty($item)){
            return false;
        }
        if (empty($url)){
            $url = $item['url'];
        }
        $data=json_decode(str_replace(array('$keyword1','$keyword2','$keyword3','$keyword4','$keyword5'),array($info['keyword1'],$info['keyword2'],$info['keyword3'],$info['keyword4'],$info['keyword5']),$item['content']),true);
        $access_token=WechatMpService::getAccessToken();
        return WechatMP::sendTemplateMessage($access_token,$info['touser'],$item['template_id'],$url,$data);
    }

    protected static function refund_apply($item,$info,$url=null){
        if (empty($item)){
            return false;
        }
        if (empty($url)){
            $url = $item['url'];
        }
        $data=json_decode(str_replace(array('$orderProductPrice','$orderProductName','$orderName'),array($info['orderProductPrice'],$info['orderProductName'],$info['orderName']),$item['content']),true);
        $access_token=WechatMpService::getAccessToken();
        return WechatMP::sendTemplateMessage($access_token,$info['touser'],$item['template_id'],$url,$data);
    }

    protected static function rate_of_refund($item,$info,$url=null){
        if (empty($item)){
            return false;
        }
        if (empty($url)){
            $url = $item['url'];
        }
        $data=json_decode(str_replace(array('$keyword1','$keyword2','$keyword3','$keyword4'),array($info['keyword1'],$info['keyword2'],$info['keyword3'],$info['keyword4']),$item['content']),true);
        $access_token=WechatMpService::getAccessToken();
        return WechatMP::sendTemplateMessage($access_token,$info['touser'],$item['template_id'],$url,$data);
    }

    protected static function refund_success($item,$info,$url=null){
        if (empty($item)){
            return false;
        }
        if (empty($url)){
            $url = $item['url'];
        }
        $data=json_decode(str_replace(array('$orderProductPrice','$orderProductName','$orderName'),array($info['orderProductPrice'],$info['orderProductName'],$info['orderName']),$item['content']),true);
        $access_token=WechatMpService::getAccessToken();
        return WechatMP::sendTemplateMessage($access_token,$info['touser'],$item['template_id'],$url,$data);
    }

    protected static function refund_reject($item,$info,$url=null){
        if (empty($item)){
            return false;
        }
        if (empty($url)){
            $url = $item['url'];
        }
        $data=json_decode(str_replace(array('$orderProductPrice','$orderProductName','$orderName'),array($info['orderProductPrice'],$info['orderProductName'],$info['orderName']),$item['content']),true);
        $access_token=WechatMpService::getAccessToken();
        return WechatMP::sendTemplateMessage($access_token,$info['touser'],$item['template_id'],$url,$data);
    }

    protected static function level_up($item,$info,$url=null){
        if (empty($item)){
            return false;
        }
        if (empty($url)){
            $url = $item['url'];
        }
        $data=json_decode(str_replace(array('$keyword1','$keyword2'),array($info['keyword1'],$info['keyword2']),$item['content']),true);
        $access_token=WechatMpService::getAccessToken();
        return WechatMP::sendTemplateMessage($access_token,$info['touser'],$item['template_id'],$url,$data);
    }

}